<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/18 11:29
 * Remark:该类主要用于对钉钉鉴权进行处理
 */

namespace App\Tools\DingTalk;

use AlibabaCloud\SDK\Dingtalk\Voauth2_1_0\Dingtalk;
use AlibabaCloud\SDK\Dingtalk\Voauth2_1_0\Models\GetAccessTokenRequest;
use App\Consts\DingTalkConsts\DingTalkConfigConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\AppFunc;
use App\Func\ResponseFunc;
use Darabonba\OpenApi\Models\Config;
use DingTalkClient;
use \Exception;
use DingTalkConstant;
use FormComponentValueVo;
use Illuminate\Http\Exceptions\HttpResponseException;
use JetBrains\PhpStorm\Pure;
use ProcessInstanceApproverVo;

class BaseDingTalk
{
    /**
     *
     * @return Dingtalk
     */
    protected static function setDingTalkClient(): Dingtalk
    {
        $config = new Config([]);
        $config->protocol = "https";
        $config->regionId = "central";
        return new Dingtalk($config);
    }

    /**
     * 新版 获取钉钉客户端
     * @return Dingtalk
     */
    public static function getDingTalkClient(): Dingtalk
    {
        return self::setDingTalkClient();
    }

    /**
     * 新版 获取钉钉 AccessToken
     * @return string|\Illuminate\Http\JsonResponse
     */
    public static function getAccessToken(): string | \Illuminate\Http\JsonResponse
    {
        $getAccessTokenRequest = new GetAccessTokenRequest([
            "appKey" => AppFunc::getDingTalkConfig()::DING_TALK_APP_KEY,
            "appSecret" => AppFunc::getDingTalkConfig()::DING_TALK_APP_SECRET
        ]);
        try {
            $accessToken = self::getDingTalkClient()->getAccessToken($getAccessTokenRequest)
                ->body
                ->accessToken;
        }
        catch (Exception $err) {
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                '',
                "钉钉客户端错误".$err->getMessage(),
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        return $accessToken;
    }

    # ========================================旧版========================================
    /**
     * 旧版 获取钉钉客户端
     * @return DingTalkClient
     */
    #[Pure] protected static function getDingTalkOldClient(): DingTalkClient
    {
        return new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
    }


    /**
     * 表单配置方法
     * @param $name
     * @param $value
     * @return FormComponentValueVo
     */
    #[Pure] protected static function setFormComponentValueVo($name, $value): FormComponentValueVo
    {
        $form_component_values = new FormComponentValueVo;
        $form_component_values->name= $name;
        if(is_array($value)){
            $value = json_encode($value,JSON_UNESCAPED_UNICODE);
        }

        $form_component_values->value= $value;
        return $form_component_values;
    }

    /**
     * 表单配置方法
     * @param $name
     * @param $value
     * @return FormComponentValueVo
     */
    #[Pure] protected static function setProcessInstanceApproverVo($taskActionType, $userIds): ProcessInstanceApproverVo
    {
        $approvers_v2 = new ProcessInstanceApproverVo;
        $approvers_v2->task_action_type= $taskActionType;
        if($taskActionType != "NONE"){
            $approvers_v2->user_ids= explode(",",$userIds);
        }else{
            $approvers_v2->user_ids= $userIds;
        }
        return $approvers_v2;
    }


}